﻿using NUnit.Framework;
using ValidationFramework.Configuration;
using System.Xml;

using ValidationFramework.Reflection;
using ValidationFramework.Specifications.Tests.Helpers;
using ValidationFramework.Specifications;

namespace ValidationFramework.Tests.Configuration.ConfigReaders
{
    [TestFixture]
    public class IsValidRuleConfigReaderTests
    {
        [Test]
        public void Should_Use_TargetRuleSet_Attribute()
        {
            // Arrange
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml("<rule typeName='IsValidRule' targetRuleSet='test' />");
            var validatorXmlElement = xmlDocument.DocumentElement;

			var configReader = new IsValidParser();

            // ACt
			var context =  new RuleContext(typeof(int), new FakeReflectionInfo());
			var validator = (IsValidSpec)configReader.GetSpecification(validatorXmlElement, context);

            // Assert
            Assert.AreEqual("test", validator.TargetRuleSet);
        }

        [Test]
        public void Should_Use_UseTargetErrorMessages_Attribute()
        {
            // Arrange
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml("<rule typeName='IsValidRule' useTargetErrorMessages='true' />");
            var validatorXmlElement = xmlDocument.DocumentElement;

            var configReader = new IsValidParser();

            // Act
			var context = new RuleContext(typeof(int), new FakeReflectionInfo());
			var validator = (IsValidSpec)configReader.GetSpecification(validatorXmlElement, context);

            // Assert
            Assert.IsTrue(validator.UseTargetErrorMessages);
        }

        [Test]
        public void Should_Create_Default_Spec_If_Given_No_Params()
        {
            // Arrange
            var xmlDocument = new XmlDocument();
            xmlDocument.LoadXml("<rule typeName='IsValidRule' />");
            var validatorXmlElement = xmlDocument.DocumentElement;

			var configReader = new IsValidParser();

            // Act
			var context = new RuleContext(typeof(int), new FakeReflectionInfo());
            var validator = (IsValidSpec)configReader.GetSpecification(validatorXmlElement, FakeRuleContext.GetContext());

            // Assert
            Assert.IsTrue(validator.IsEquivalentTo(new IsValidSpec()));
        }
    }
}
